package com.winit.agent.web;

import com.winit.agent.manager.AgentManager;
import com.winit.agent.manager.AgentWorkspaceManager;
import com.winit.agent.model.Agent;
import com.winit.agent.model.AgentWorkspace;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "agent", tags = {"agent"}, description = "agent")
@RestController
@RequestMapping("/api/v1/agent")
public class AgentController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(AgentController.class);

    @Autowired
    private AgentManager agentManager;
    @Autowired
    private AgentWorkspaceManager agentWorkspaceManager;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询agent列表")
    @ResponseBody
    public Map getAgent(
            @ApiParam(value = "agentId", required = false) @RequestParam(value = "agentId", required = false) String agentId,
            @ApiParam(value = "agentIp", required = false) @RequestParam(value = "agentIp", required = false) String agentIp) {
        ExecuteResult executeResult = this.agentManager.getAgent(agentId, agentIp);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc(), executeResult.getData());
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "更新agent")
    @ResponseBody
    public Map updateAgent(@RequestBody Agent agent) {
        ExecuteResult executeResult = this.agentManager.updateAgent(agent);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc(), executeResult.getData());
    }

    @RequestMapping(value = "/operate", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "操作agent")
    @ResponseBody
    public Map operateAgent( @ApiParam(value = "agentId", required = false) @RequestParam(value = "agentId", required = true) String agentId,
                             @ApiParam(value = "operation, 1（默认）-删除", required = false) @RequestParam(value = "operation", required = false) Integer operation) {
        if (StringUtils.isEmpty(agentId)){
            return this.getRtn(BaseController.REQUEST_ERROR, "agentId cannot be null.");
        }
        if (null == operation){
            operation = 1;
        }
        ExecuteResult executeResult = this.agentManager.operateAgent(agentId, operation);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc(), executeResult.getData());
    }


    @RequestMapping(value = "/ws/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询agent列表")
    @ResponseBody
    public Map getWorkspace( @ApiParam(value = "agentId", required = false) @RequestParam(value = "agentId", required = false) String agentId,
                             @ApiParam(value = "isDefault, 0-是，1-否", required = false) @RequestParam(value = "isDefault", required = false) String isDefault,
                             @ApiParam(value = "wsId", required = false) @RequestParam(value = "wsId", required = false) String wsId) {
        if ("0".equalsIgnoreCase(isDefault)){
            agentId = null;
            wsId = null;
        }
        List<AgentWorkspace> list = this.agentWorkspaceManager.getAgentWorkspace(agentId,wsId, isDefault);
        return this.getRtn(BaseController.REQUEST_SUCCESS, "success", list);
    }


    @RequestMapping(value = "/ws/update", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "更新agent workspace")
    @ResponseBody
    public Map updateAgentWorkspace(@RequestBody AgentWorkspace agentWorkspace) {
        ExecuteResult executeResult = this.agentWorkspaceManager.updateAgentWorkspace(agentWorkspace);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc(), executeResult.getData());
    }



    @RequestMapping(value = "/ws/operate", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "操作agent workspace")
    @ResponseBody
    public Map operateAgentWorkspace( @ApiParam(value = "wsId", required = false) @RequestParam(value = "wsId", required = true) String wsId,
                             @ApiParam(value = "operation, 1（默认）-删除", required = false) @RequestParam(value = "operation", required = false) Integer operation) {
        if (StringUtils.isEmpty(wsId)){
            return this.getRtn(BaseController.REQUEST_ERROR, "agentId cannot be null.");
        }
        if (null == operation){
            operation = 1;
        }
        ExecuteResult executeResult = this.agentWorkspaceManager.operateAgentWorksapce(wsId, operation);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc(), executeResult.getData());
    }
}
